﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kovan.Model.Entity.UserCenter;
using Kovan.Model.ViewModel.UserCenter;

namespace Kovan.DAL.UserCenter
{
    public class DActAuthority : IDAL<act_authority>
    {
        /// <summary>
        /// 根据模块ID获取列表
        /// </summary>
        /// <param name="moduleid"></param>
        /// <returns></returns>
        public IEnumerable<act_authority> GetListByModuleId(long moduleid)
        {
            using (var db = new MysqlContext())
            {
                return db.act_authority.Where(c => c.module_id.Value == moduleid).ToList();
            }
        }
        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        public act_authority GetModel(long id)
        {
            using (var db = new MysqlContext())
            {
                return db.act_authority.FirstOrDefault(c => c.authority_id == id);
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
        public int Add(act_authority model)
        {
            using (var db = new MysqlContext())
            {
                db.act_authority.Add(model);
                return db.SaveChanges();
            }
        }

        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="moduleid">模块ID</param>
        /// <param name="codes">权限集合（模块ID未赋值）</param>
        /// <returns></returns>
        public int BatchAdd(long moduleid, IEnumerable<act_authority> codes)
        {
            using (var db = new MysqlContext())
            {
                foreach (var model in codes)
                {
                    model.module_id = moduleid;
                    db.act_authority.Add(model);
                }
                return db.SaveChanges();
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        public int Delete(long id)
        {
            using (var db = new MysqlContext())
            {
                var model = db.act_authority.FirstOrDefault(a => a.authority_id == id);
                db.act_authority.Remove(model);
                return db.SaveChanges();
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="moduleid">模块ID</param>
        /// <returns></returns>
        public int DeleteByModuleId(long moduleid)
        {
            using (var db = new MysqlContext())
            {
                var list = db.act_authority.Where(a => a.module_id == moduleid);
                if(list!=null && list.Count()>0)
                {
                    foreach (var item in list)
                    {
                        db.act_authority.Remove(item);
                    }
                    return db.SaveChanges();
                }
                return 1;
            }
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int BatchDelete(IEnumerable<string> ids)
        {
            using (var db = new MysqlContext())
            {
                foreach (var id in ids)
                {
                    var authorityid = long.Parse(id);
                    var model = db.act_authority.FirstOrDefault(a => a.authority_id == authorityid);
                    if (model != null)
                    {
                        db.act_authority.Remove(model);
                    }
                }
                return db.SaveChanges();
            }
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
        public int Update(act_authority model)
        {
            using (var db = new MysqlContext())
            {
                db.Set<act_authority>().Attach(model);
                db.Entry(model).State = System.Data.EntityState.Modified;
                return db.SaveChanges();
            }
        }
    }
}
